package cn.devzyh.service.impl;

import cn.devzyh.dao.PromotionAdMapper;
import cn.devzyh.domain.PromotionAd;
import cn.devzyh.domain.PromotionAdVo;
import cn.devzyh.service.PromotionAdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PromotionAdServiceImpl implements PromotionAdService {

    @Autowired
    private PromotionAdMapper promotionAdMapper;

    @Override
    public PageInfo findAllByPage(PromotionAdVo promotionAdVo) {

        PageHelper.startPage(promotionAdVo.getCurrentPage(), promotionAdVo.getPageSize());
        List<PromotionAd> ads = promotionAdMapper.findAll();

        PageInfo<PromotionAd> pageInfo = new PageInfo<>(ads);

        return pageInfo;
    }

    @Override
    public void savePromotionAd(PromotionAdVo promotionAdVo) {

        PromotionAd ad = new PromotionAd();

        // 复制数据
        BeanUtils.copyProperties(promotionAdVo, ad);

        // 设置默认数据
        Date now = new Date();
        ad.setCreateTime(now);
        ad.setUpdateTime(now);

        promotionAdMapper.savePromotionAd(ad);
    }

    @Override
    public void updatePromotionAd(PromotionAdVo promotionAdVo) {

        PromotionAd ad = new PromotionAd();

        // 复制数据
        BeanUtils.copyProperties(promotionAdVo, ad);

        // 设置默认数据
        ad.setUpdateTime(new Date());

        promotionAdMapper.updatePromotionAd(ad);
    }

    @Override
    public PromotionAd findOneById(Integer id) {

        return promotionAdMapper.findOneById(id);
    }

    @Override
    public void updatePromotionAdStatus(Integer id, Integer status) {

        PromotionAd ad = new PromotionAd();
        ad.setId(id);
        ad.setStatus(status);
        ad.setUpdateTime(new Date());

        promotionAdMapper.updatePromotionAdStatus(ad);
    }
}
